<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\ShopDish;
use App\Models\ShopDishAttribute;
use App\Repositories\TakeoutRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class TakeoutController extends Controller
{
    protected $takeoutRepository;
    /**
     * TakeoutController constructor.
     * @param $AddressRepository
     */
    public function __construct(TakeoutRepository $takeoutRepository){
        $this->takeoutRepository = $takeoutRepository;
    }

    //获取菜单
    public function getshopMenu(Request $request){

        return show("200","成功",$this->takeoutRepository->getshopMenu($request->id));

    }
    //根据menu id获取菜式
    public function getIdshopdish(Request $request){

        return show("200","成功",$this->takeoutRepository->getIdshopdish($request->id));

    }
    //确定下单
    public function addOrder(Request $request){

        return  $this->takeoutRepository->addOrder($request->cart,$request->coupon_id);

    }
    //小程序支付
    public function addProgramOrder(Request $request){

        return  $this->takeoutRepository->addOrderProgram($request->cart,$request->coupon_id);

    }
    //申请退款
    public function  applyForRefund(Request $request){
        return $this->takeoutRepository->applyForRefund($request->id,$request->token);
    }

    //确认收货
    public function  confirmReceipt(Request $request){
        return $this->takeoutRepository->confirmReceipt($request->id,$request->token);
    }

    //确认接单
    public function  sureOrder(Request $request){
        return $this->takeoutRepository->sureOrder($request->id,$request->token);
    }

    //确认接单
    public function sureForRefund(Request $request){
        return $this->takeoutRepository->sureForRefund($request->id,$request->token);
    }

    // 根据不同类型获取不同推荐菜式
    public function getRecommendDishes(Request $request){
        $rules = [
            'type'=>'required|integer|in:1,2,3',         // 1 推荐 2 热卖 3 特价
        ];
        $message = [
            'type.*'=>'请选择正确的类型',
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $type = $request->type;
        //获取相应的菜式
        switch ($type){
            case 1:
                $dishes = ShopDish::where('recommend',1)->where('status',1)->get();
                break;
            case 2:
                $dishes = ShopDish::where('status',1)->orderBy('sales','desc')->get();
                break;
            case 3:
                $dishes = ShopDish::where('discount','<',1)->where('status',1)->orderBy('discount','desc')->get();
                break;
            default:
                return show(202,"暂无菜式");
                break;
        }
        // 对菜式作统一处理
        if ($dishes->isNotEmpty()){
            $dishes = $dishes->toArray();
        }else{
            return show(202,"暂无菜式");
        }
        //判断多规格
        foreach($dishes as $k=>$v){
            if (strpos($v['thumb'],';')){
                $res = explode(';',$v['thumb']);
                $dishes[$k]['thumb']=$res[0];
            }
            if (strpos($v['thumb'],',')){
                $res = explode(',',$v['thumb']);
                $dishes[$k]['thumb']=$res[0];
            }
            if($v['is_attr']==1){
                $dishes[$k]['attribute'] = ShopDishAttribute::where('dish_id',$v['id'])->get();
                //处理多规格
                for($y=0;$y<count($dishes[$k]['attribute']);$y++)
                {
                    $spec=explode(",", $dishes[$k]['attribute'][$y]['spec']);
                    foreach($spec as $k2=>$v2)
                    {
                        $spec[$k2] = explode("|",$v2);
                        $specend[$k2]['spec_name'] = $spec[$k2][0];
                        $specend[$k2]['spec_price'] = $spec[$k2][1];
                    }
                    $dishes[$k]['attribute'][$y]['spec']=$specend;
                }
            }
        }
        return show(200,"成功",$dishes);
    }

}
